<?php
namespace api\modules\v1\controllers\trade;
use Yii;
use api\modules\v1\controllers\CoreController;
use common\models\lowcarbon\LowCarbonTravelModel;
/*
	低碳环保
*/

class LowcarbonController extends CoreController
{

	/*
		低碳出行
		今日出行步数
	*/
	public function actionGet_step_nums() 
	{
		$date = date("Y-m-d");
		$where = ['member_id' => $this->_memberId,'date'=>$date];
		$data = LowCarbonTravelModel::find()->where($where)->asarray()->one();
		$stepNum = isset($data['step_number'])?$data['step_number']:0;
		$this->out('今日步数',['step_num'=>$stepNum]);
	}

	/*
		更新步数 

		* stepNum 当时步数
		* distance 距离
	*/
	public function actionUpdate_step_nums()
	{
		// $this->write_log($this->request,'temp');
		if($this->request('stepNum',0)<=0) $this->out('更新成功');
		$date = date("Y-m-d");
		$where = ['member_id' => $this->_memberId,'date'=>$date];
		$model = LowCarbonTravelModel::findOne($where);
		if($model){
			$model->step_number = $this->request('stepNum',0);
			$model->distance = $this->request('distance',0);
			$model->update_time = time();
			$flag = $model->save();

		}else{
			$model = new LowCarbonTravelModel;
			$model->member_id = $this->_memberId;
			$model->date = $date;
			$model->step_number = $this->request('stepNum',0);
			$model->distance = $this->request('distance',0);
			$model->update_time = time();
			$flag = $model->save();
		}
		if($flag) $this->out('更新成功');
		$this->error('更新失败');
	}



}
